﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Htse.Blue.Models;
using System.Data.Linq;

namespace Htse.Blue.Data
{
	public class CityDistanceRepository :
		LinqToSqlRepositoryBase<CityDistance, int, BlueEntityModelDataContext>,
		ICityDistanceRepository
	{
		public override CityDistance GetById(int id)
		{
			return dataContext.CityDistances.SingleOrDefault(c => c.Id == id);
		}

		#region ICityDistanceRepository Members

		public bool ExistWithSameCities(CityDistance cityDistance)
		{
			var result =
				from cd in dataContext.CityDistances
				where
					(cd.City1.Id == cityDistance.FirstCityId && cd.City2.Id == cityDistance.SecondCityId) ||
					(cd.City1.Id == cityDistance.SecondCityId && cd.City2.Id == cityDistance.FirstCityId)
				select cd;

			if (result.Count() > 0)
			{
				return true;
			}
			return false;
		}

		#endregion
	}
}
